<?php defined('WWW_ROOT') or die(header("HTTP/1.1 404 Not Found")); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <title>Title</title>
    <script type="text/javascript"
            src="http://api.map.baidu.com/api?v=3.0&ak=dI7VNXaXKiEU5fj66VO7A97p6SFvjgbY"></script>
    <style type="text/css">
        html {
            height: 100%
        }

        body {
            height: 100%;
            margin: 0px;
            padding: 0px
        }

        #container {
            height: 100%
        }
    </style>
</head>
<body>
<div id="container">

</div>
<script type="text/javascript">
    var map = new BMap.Map("container");
    var point = new BMap.Point(120.187537, 30.194665);

    map.centerAndZoom(point, 16);
    var polygon = new BMap.Polygon([
        new BMap.Point(120.187537, 30.194665),
        new BMap.Point(120.185696, 30.194157),
        new BMap.Point(120.186046, 30.192394),
        new BMap.Point(120.187151, 30.19358),
        new BMap.Point(120.188013, 30.193151)
    ], {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5});  //创建多边形
    map.addOverlay(polygon);   //增加多边形
    //alert(polygon.containsPoint(new BMap.Point(120.187025,30.192792)));
    var at_point = new BMap.Point(120.18747,30.194583);
    var marker = new BMap.Marker(at_point);  // 创建标注
    map.addOverlay(marker);               // 将标注添加到地图中
    marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画
    console.log(isInsidePolygon(at_point, polygon.getPath()));
    polygon.addEventListener('click', function (ev) {
        console.log(ev);
    });

    /**
     * 计算一个点是否在多边形里
     * @param {Object} pt 标注点
     * @param {Object} poly 多边形数组
     */
    function isInsidePolygon(pt, poly) {
        for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
            ((poly[i].lat <= pt.lat && pt.lat < poly[j].lat) || (poly[j].lat <= pt.lat && pt.lat < poly[i].lat)) && (pt.lng < (poly[j].lng - poly[i].lng) * (pt.lat - poly[i].lat) / (poly[j].lat - poly[i].lat) + poly[i].lng) && (c = !c);
        return c;
    }
</script>
</body>
</html>
